/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.inventory.service.impl;

import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.suli.inventory.domain.InventoryDetail;
import me.zhengjie.suli.inventory.repository.InventoryDetailRepository;
import me.zhengjie.suli.inventory.service.InventoryDetailService;
import me.zhengjie.suli.inventory.service.dto.InventoryDetailDto;
import me.zhengjie.suli.inventory.service.dto.InventoryDetailQueryCriteria;
import me.zhengjie.suli.inventory.service.mapstruct.InventoryDetailMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author WYH
* @date 2024-02-01
**/
@Service
@RequiredArgsConstructor
public class InventoryDetailServiceImpl implements InventoryDetailService {

    private final InventoryDetailRepository inventoryDetailRepository;
    private final InventoryDetailMapper inventoryDetailMapper;

    @Override
    public Map<String,Object> queryAll(InventoryDetailQueryCriteria criteria, Pageable pageable){
        Page<InventoryDetail> page = inventoryDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(inventoryDetailMapper::toDto));
    }

    @Override
    public List<InventoryDetailDto> queryAll(InventoryDetailQueryCriteria criteria){
        return inventoryDetailMapper.toDto(inventoryDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public InventoryDetailDto findById(String id) {
        InventoryDetail inventoryDetail = inventoryDetailRepository.findById(id).orElseGet(InventoryDetail::new);
        ValidationUtil.isNull(inventoryDetail.getId(),"InventoryDetail","id",id);
        return inventoryDetailMapper.toDto(inventoryDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public InventoryDetailDto create(InventoryDetail resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return inventoryDetailMapper.toDto(inventoryDetailRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(InventoryDetail resources) {
        InventoryDetail inventoryDetail = inventoryDetailRepository.findById(resources.getId()).orElseGet(InventoryDetail::new);
        ValidationUtil.isNull( inventoryDetail.getId(),"InventoryDetail","id",resources.getId());
        inventoryDetail.copy(resources);
        inventoryDetailRepository.save(inventoryDetail);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            inventoryDetailRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<InventoryDetailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (InventoryDetailDto inventoryDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("储位", inventoryDetail.getStorageLocation());
            map.put("头id", inventoryDetail.getInventoryId());
            map.put("等级", inventoryDetail.getGrade());
            map.put("单位", inventoryDetail.getUnit());
            map.put("批次", inventoryDetail.getBatch());
            map.put("仓库类型", inventoryDetail.getWarehouseType());
            map.put("库位", inventoryDetail.getWarehouseLocation());
            map.put("入库天数", inventoryDetail.getSurvivalDays());
            map.put("物料组", inventoryDetail.getMaterialGroup());
            map.put("VMI标志", inventoryDetail.getVmiSign());
            map.put("供应商", inventoryDetail.getSupplier());
            map.put("vmi入库时间", inventoryDetail.getVmiCreattime());
            map.put("vmi滞留天数", inventoryDetail.getVmiRetentionDays());
            map.put("来源单据", inventoryDetail.getSourceDocuments());
            map.put("单据类型", inventoryDetail.getDocumentsType());
            map.put("数量", inventoryDetail.getNumber());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}